#include <iostream>
#include <queue>
#include <unordered_map>
#include <string>

using namespace std;

// 定义二叉树节点结构
struct TreeNode {
    char val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(char x) : val(x), left(NULL), right(NULL) {}
};

// 创建二叉树
TreeNode* createTree() {
    unordered_map<char, TreeNode*> nodeMap;
    string line;
    TreeNode* root = NULL;

    while (getline(cin, line)) {
        if (line == "#") break;

        char parentVal = line[0];
        TreeNode* parent;
        if (nodeMap.find(parentVal) == nodeMap.end()) {
            parent = new TreeNode(parentVal);
            nodeMap[parentVal] = parent;
        } else {
            parent = nodeMap[parentVal];
        }

        if (!root) root = parent;

        if (line.size() > 2) {
            char leftVal = line[2];
            if (leftVal != '0') {
                TreeNode* leftChild;
                if (nodeMap.find(leftVal) == nodeMap.end()) {
                    leftChild = new TreeNode(leftVal);
                    nodeMap[leftVal] = leftChild;
                } else {
                    leftChild = nodeMap[leftVal];
                }
                parent->left = leftChild;
            }

            if (line.size() > 4) {
                char rightVal = line[4];
                if (rightVal != '0') {
                    TreeNode* rightChild;
                    if (nodeMap.find(rightVal) == nodeMap.end()) {
                        rightChild = new TreeNode(rightVal);
                        nodeMap[rightVal] = rightChild;
                    } else {
                        rightChild = nodeMap[rightVal];
                    }
                    parent->right = rightChild;
                }
            }
        }
    }

    return root;
}

// 交换左右子树
void swapChildren(TreeNode* node) {
    if (!node) return;
    swap(node->left, node->right);
    swapChildren(node->left);
    swapChildren(node->right);
}

// 层次遍历输出
void levelOrderTraversal(TreeNode* root) {
    if (!root) return;

    queue<TreeNode*> q;
    q.push(root);

    while (!q.empty()) {
        TreeNode* current = q.front();
        q.pop();

        cout << current->val;

        if (current->left) {
            cout << " " << current->left->val;
            q.push(current->left);
        } else {
            cout << " 0";
        }

        if (current->right) {
            cout << " " << current->right->val;
            q.push(current->right);
        } else {
            cout << " 0";
        }

        cout << endl;
    }
}

int main() {
    TreeNode* root = createTree();
    swapChildren(root);
    levelOrderTraversal(root);

    return 0;
}